package com.obas.component.tree {
   import com.ease.framework.CommandCompletionEvent;
   import com.obas.component.tree.treeclass.Node;
   import com.obas.constants.TestingLibConstants;
   import com.obas.constants.StringConstants;
   import com.obas.event.TestPlanOperationEvent;
   import com.obas.flex.common.util.ResourceUtil;
   import com.obas.model.business.TestPlan;
   import com.obas.model.business.TestTask;
   import com.obas.model.business.TestTaskItem;
   import com.obas.view.menu.TestPlanMenu;
   import com.ease.component.controls.Menu;

   import mx.collections.ArrayCollection;
   import mx.core.Container;

   [Event(name="{com.obas.event.TestPlanOperationEvent.REQUEST_ID}", type="com.obas.event.TestPlanOperationEvent")]
   public class TestPlanTree extends LoadingTree {

      override protected function buildMenu(container:Container):void {
         var testPlanMenu:Menu = new TestPlanMenu();
         container.addChild(testPlanMenu);
         _menuMap[TestingLibConstants.ITEMTYPE_TEST_PLAN] = testPlanMenu;
      }

      //点击展开节点，获取节点下的node
      override protected function callRemoteService(node:Node):void {
         var entity:* = node.entity;
         if (entity is TestPlan) {
            dispatchEvent(new TestPlanOperationEvent([entity.id, node], TestPlanOperationEvent.GET_TASK));
         }
         else if (entity is TestTask) {
            dispatchEvent(new TestPlanOperationEvent([entity.id, node], TestPlanOperationEvent.GET_TASK_ITEM));
         }
      }

      [ResponseHandler("{com.obas.event.TestPlanOperationEvent.RETURN_ID}")]
      public function onTestPlanOperationEvent(event:CommandCompletionEvent):void {
         var loadingTreeEvent:TestPlanOperationEvent = event.request as TestPlanOperationEvent;
         var requestNode:Node = loadingTreeEvent.spec[1];

         if (event.error) {
            const errorMsg:String = ResourceUtil.getErrorString(StringConstants.EXPLORER_ERROR, event.error);
            addErrorStat(requestNode, errorMsg);
            return ;
         }

         var result:ArrayCollection = event.result as ArrayCollection;
         removeNodeStat(requestNode);
         var childNode:Node = null;
         for each (var childEntity:Object in result) {
            childNode = nodeFactory.createNode(childEntity);
            childNode.parentNode = requestNode;
            childNode.type = getNodeType(childEntity);
            if (childNode.type == TestingLibConstants.ITEMTYPE_TASK_ITEM) {
               childNode.children = null;
            }
            requestNode.children.addItem(childNode);
         }

         requestNode.initialized = true;
         expandItem(requestNode, false);
         expandItem(requestNode, true);
      }

      public function getNodeType(item:*):String {
         if (item is TestPlan) {
            return TestingLibConstants.ITEMTYPE_TEST_PLAN;
         }
         else if (item is TestTask) {
            return TestingLibConstants.ITEMTYPE_TEST_TASK;
         }
         else if (item is TestTaskItem) {
            return TestingLibConstants.ITEMTYPE_TASK_ITEM;
         }
         return "";
      }
   }
}